
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../../event/scan_event.dart';
import '../../main.dart';

class QrModel{

  show(TextEditingController controller,FocusNode focusNode){
    return Container(
        margin: EdgeInsets.only(left: 16.w,right: 16.w),
        width: double.infinity,
        decoration: BoxDecoration(
          color: const Color.fromRGBO(239, 244, 255, 1),
          borderRadius: BorderRadius.all(Radius.circular(16.r)),
        ),
        padding: EdgeInsets.only(left:10.r),
        child: Row(
          children: [
            Image.asset("assets/images/scan.png",width:20.w,fit: BoxFit.fitWidth,),
            SizedBox(width: 5.w,),
            SizedBox(
              width: 220.w,
              child: TextField(
                minLines: 1,
                controller: controller,
                textInputAction: TextInputAction.done,
                decoration: InputDecoration(
                    contentPadding: EdgeInsets.only(top: 8.r,bottom: 8.r),
                    isCollapsed: true,
                    hintText: "可扫描图书条形码进行借书",
                    border: InputBorder.none,
                    hintStyle: TextStyle(color: Colors.blue,fontSize: 8.sp)
                ),
                onChanged: (value){
                  _send(value);
                },
                style: TextStyle(color: Colors.blue,fontSize: 8.sp),
                autofocus: true,
                focusNode:focusNode,
              ),

            ),
          ],
        )
    );
  }

  Timer? _timer;
  _send(value){
    _timer?.cancel();
    _timer = null;
    _timer = Timer.periodic(const Duration(milliseconds: 500), (timer) {
      eventFactory.fire(ScanEvent(value));
      _timer?.cancel();
      _timer = null;
    });

  }
}

